﻿<?php
require_once 'Constant.php' ;
class User_PositionController extends AppController
{
    public function listAction()
    {
        $objPositionTable = new Cts_Db_Position() ;
        $aParams = $this->getRequest()->getParams() ;
        $nStart = isset( $aParams['start'] ) ? $aParams['start'] : 0 ;
        $nLimit = isset( $aParams['limit'] ) ? $aParams['limit'] : 0 ;
        
        try
        {   
            $aPositions = $objPositionTable->getList( '1=1' , $nStart , $nLimit ) ;
            
            //Zend_Debug::dump( $aUsers ) ;
            
            $nPositionCount = count( $aPositions ) ;
            
            $aResult = array(  
                'datacount'  => $nPositionCount ,
                'datas'      => $aPositions
            ) ;
            
            $strResult = Zend_Json::encode( $aResult ) ;
            echo $strResult ;
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
        }
    }   
    
    public function addAction()
    {
        $objPositionTable = new Cts_Db_Position() ;
        $aParams = $this->getRequest()->getParams() ;
        $strPositionName = trim( $aParams['strPositionName'] ) ;
        $strLocator = 'name = \'' . $strPositionName . '\'' ;
        
        try
        {
            $aPosition = $objPositionTable->getList( $strLocator , NULL , NULL ) ;
            if( empty( $aPosition ) )
            {
                $aInsertData = array(
                    'name'   =>   $strPositionName 
                );        
                $nResult = $objPositionTable->insert( $aInsertData ) ;
                if( $nResult > 0 )
                {
                    $strResult = '添加新职位‘ '.$strPositionName.'’ 成功' ;
                    Cts_Json::echoResult( POSITION_ADD_SUCCESSFUL , $strResult ) ;
                    //write operation into db
    		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;   
                }
                else
                {
                    $strResult = '添加职位名称 '.$strPositionName.' 失败，请检查数据库连接。' ;
                    Cts_Json::echoResult( POSITION_ADD_FAILD , $strResult ) ;  
                    //write operation into db
    		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
                }
            }
            else
            {
                $strResult = $strPositionName.'该职位已经存在。请重新输入。' ;
                Cts_Json::echoResult( POSITION_IS_ALREADE_HAVE , $strResult ) ;
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }
    }
    
    public function updateAction()
    {
        $objPositionTable = new Cts_Db_Position() ;
        $aParams = $this->getRequest()->getParams() ;
        $strPositionName = trim( $aParams['strPositionName'] ) ;
        $nPositionId = intval( $aParams['nPositionId'] ) ;
        
        try
        {
            if( !empty( $strPositionName ) && $nPositionId > 0 )
            {
                $aUpdateData = array(
                    'name'   =>   $strPositionName 
                );        
                $strLocator = 'id = '.$nPositionId ;
                $nResult = $objPositionTable->update( $aUpdateData , $strLocator ) ;
                if( $nResult > 0 )
                {
                    $strResult = '修改职位名称‘ '.$strPositionName.'’ 成功' ;
                    Cts_Json::echoResult( POSITION_UPDATE_SUCCESSFUL , $strResult ) ;
                    //write operation into db
    		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;   
                }
                else
                {
                    $strResult = '修改职位名称 '.$strPositionName.' 失败，请检查数据库连接。' ;
                    Cts_Json::echoResult( POSITION_UPDATE_FAILD , $strResult ) ;  
                    //write operation into db
    		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
                }
            }
            else
            {
                $strResult = '提交的参数不合法，请重新输入信息。' ;
                Cts_Json::echoResult( POSITION_PARAMS_INVALIDED , $strResult ) ;  
                //write operation into db
		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }    
    }
    
    public function deleteAction()
    {
        $objPositionTable = new Cts_Db_Position() ;
        $objLoginTable = new Cts_Db_Login() ;
        $aParams = $this->getRequest()->getParams() ;
        $nPositionId = intval( $aParams['nPositionId'] ) ;
        
        try
        {
            if( $nPositionId > 0 )
            {       
                $strLocator = 'position_id = '.$nPositionId ;
                $nResult = $objLoginTable->getCount( $strLocator ) ;
                if( $nResult > 0 )
                {
                    $strResult = '该职位名称在被使用中，无法删除。' ;
                    Cts_Json::echoResult( POSITION_IN_USED , $strResult ) ;
                }
                else
                {
                    $strLocator = 'id = '.$nPositionId ;
                    if( $objPositionTable->delete( $strLocator ) )
                    {
                        $strResult = '删除职位名称成功。' ;
                        Cts_Json::echoResult( POSITION_DELETE_SUCCESSFUL , $strResult ) ;  
                        //write operation into db
        		        $this->writeOpLog( '成功' , __CLASS__ , __FUNCTION__) ;     
        		    }
        		    else
        		    {
        		        $strResult = '删除职位名称失败。' ;
                        Cts_Json::echoResult( POSITION_DELETE_FAILD , $strResult ) ;  
                        //write operation into db
        		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ; 
        		    }
                }
            }
            else
            {
                $strResult = '提交的参数不合法，请重新选择要删除的信息。' ;
                Cts_Json::echoResult( POSITION_PARAMS_INVALIDED , $strResult ) ;  
                //write operation into db
		        $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;     
            }
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
            //write operation into db
            $this->writeOpLog( '失败' , __CLASS__ , __FUNCTION__) ;
        }     
    }
    
    public function getlistAction()
    {
        $objPositionTable = new Cts_Db_Position() ;
        $aResult = array() ;
        
        try
        {   
            $aPositions = $objPositionTable->getList( '1=1' , NULL , NULL ) ;
            
            foreach( $aPositions as $key => $value )
            {
                $aTemp = array(
                    'position_id'    => $value['id'] ,
                    'position_name'  => $value['name']
                );
                array_push( $aResult , $aTemp ) ;
                
            }
            
            $aResult = array(  
                'records'  => $aResult 
            ) ;
            
            $strResult = Zend_Json::encode( $aResult ) ;
            echo $strResult ;
        }
        catch( Zend_Exception $e )
        {
            //write the error info to db
            $this->writeErrLog( $e->getMessage() , get_class($this) , __FUNCTION__ , print_r( $e , true ) ) ;
        }
    }
}